package cn.zzqayy.steward;

import cn.zzqayy.steward.enums.ResultEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一返回的对象
 * @author zzqayy
 * @date 2020-03-20
 */
public class ResultJson<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private int code;

    /**
     * 返回的错误信息
     */
    private String msg;

    /**
     * 返回的结果
     */
    private T data;

    public ResultJson(){}

    public ResultJson(int code, String msg){
        this.code = code;
        this.msg = msg;
    }

    public ResultJson(int code, String msg, T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 通过枚举返回结果
     * @param resultEnum
     * @param <T>
     * @return
     */
    public static <T> ResultJson RESULT(ResultEnum resultEnum){
        return new ResultJson(
                resultEnum.getCode(),
                resultEnum.getMsg()
        );
    }

    /**
     * 通过枚举返回结果
     * @param resultEnum
     * @param <T>
     * @return
     */
    public static <T> ResultJson RESULT(ResultEnum resultEnum, T data){
        return new ResultJson(
                resultEnum.getCode(),
                resultEnum.getMsg(),
                data
        );
    }

    /**
     * 成功
     * @param <T>
     * @return
     */
    public static <T> ResultJson SUCCESS(){
        return ResultJson.RESULT(ResultEnum.SUCCESS);
    }

    /**
     * 成功
     * @param <T>
     * @return
     */
    public static <T> ResultJson SUCCESS(T data){
        return ResultJson.RESULT(ResultEnum.SUCCESS, data);
    }

    /**
     * 失败
     * @param <T>
     * @return
     */
    public static <T> ResultJson ERROR(){
        return ResultJson.RESULT(ResultEnum.ERROR);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


}
